﻿function getType(type, format) {
	var newType = '';
	if (format === '($date-time)') {
		newType = 'number';
	} else {
		switch (type) {
			case 'integer':
			case 'number':
				newType = 'number';
				break;
			  case 'boolean':
				newType = 'boolean';
				break;
			case 'string':
				newType = 'string';
				break;
			default:
				break;
		}
	}
	return newType;
}

function filter(target) {
   var a = '';
	Array.from(target.getElementsByTagName('tr')).forEach(element => {
		var key = element
			.getElementsByTagName('td')[0]
			.textContent.replace(/<!--.+?-->/g, '');

		var info = element.getElementsByTagName('td')[1];
		var propType = info.getElementsByClassName('prop-type')[0];
		var propTypeFomart = info.getElementsByClassName('prop-format')[0];
		var propTypeStr = propType && propType.textContent;
		var propTypeFomartStr =
			propTypeFomart && propTypeFomart.textContent.replace(/<!--.+?-->/g, '');
		var type = getType(propTypeStr, propTypeFomartStr);
		var descEle = info.getElementsByClassName('markdown')[0];
		var desc = descEle && descEle.textContent.replace(/\n/g, '');
		a += descEle?`
			/*** ${desc} */
			${key}: ${type};`:`
			${key}: ${type};`;
	});
	console.log(a)
	return a
}


document.addEventListener('DOMContentLoaded', function(){
	// 监听来自content-script的消息
	document.body.oncontextmenu = (event) => {
		let data = event.target.offsetParent.tagName==='TABLE'?filter(event.target.offsetParent):''
		chrome.runtime.sendMessage({data});
	}
});